import { loginApi } from '@/api/user'
import { getToken, removeToken, setToken } from '@/utils/auth'
// import { resetRouter } from '@/router'
import { Message } from 'element-ui'

const getDefaultState = () => {
  return {
    token: getToken(),
    userInfo: {}
  }
}

const state = getDefaultState()

const mutations = {
  SET_TOKEN: (state, token) => {
    setToken(token)
    state.token = token
  },
  REMOVE_TOKEN(state) {
    removeToken()
    state.token = null
  }
}

const actions = {
  async login(context, data) {
    // todo: 调用登录接口
    const res = await loginApi(data)
    // 返回一个token 123456
    context.commit('SET_TOKEN', res.token)
    Message.success('登录成功')
  },
  logout(context) {
    context.commit('REMOVE_TOKEN')
    Message.success('退出成功')
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
